﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MSH.Common.Classes;
using WP7Client.Helpers;
using WP7Client.ServerManager;
using MSH.Common.DAO.Results;

namespace WP7Client.Controller
{
    public class ActivateNewPasswordController : BaseController
    {
        #region Members

        private String _Username;
        private String _Code;

        #endregion

        #region Get&Set

        public ICommand ActivateCommand 
        { 
            get; 
            internal set; 
        }

        public String Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        public String Code
        {
            get { return _Code; }
            set { _Code = value; }
        }


        #endregion

        public ActivateNewPasswordController()
        {
            ActivateCommand = new RelayCommand(ActivateResetExecute);
        }

        /// <summary>
        /// Activeren van het nieuwe wachtwoord
        /// </summary>
		private void ActivateResetExecute()
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Code))
            {
                GlobalHelper.ShowMessage("Error", "All fields need to be filled in.");
                return;
            }

            Loading = true;
            HttpGet activateResetHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "ActivatePasswordRequest/{0}/{1}", Username, Code), typeof(ActivatePasswordRequestResult));
            activateResetHttpGet.FinishedGet += activateResetHttpGet_FinishedGet;
        }

        void activateResetHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return; //Eventueel foutmelding

                ActivatePasswordRequestResult result = (ActivatePasswordRequestResult)get.resultObject;

                switch (result.Result)
                {
                    case ActivatePasswordRequestResultEnum.UserNotExists:
                        GlobalHelper.ShowMessage("Error", "User does not exist. \nPlease try again.");
                        break;
                    case ActivatePasswordRequestResultEnum.Succes:
                        GlobalHelper.ShowMessage("Information", "Your password has been reset.");
                        NavigationHelper.NavigateTo("/UserInterface/Pages/LoginPage.xaml");
                        break;
                    case ActivatePasswordRequestResultEnum.NoOpenPasswordRequest:
                        GlobalHelper.ShowMessage("Error", "No password change request exists for this user.");
                        break;
                    case ActivatePasswordRequestResultEnum.CodeNotValid:
                        GlobalHelper.ShowMessage("Error", "Activation code is not valid. \nPlease try again.");
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }
    }
}
